﻿/*
 * shaim
 * http://shaim.net
 * Copyright ©2005-2008, shaim dev team
 * Licensed under the Massachusetts Institute of Technology (MIT) License
 * http://www.opensource.org/licenses/mit-license.php
 * 
 */

using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;

namespace csammisrun.shaim
{
    /// <summary>
    /// A group defined by the core that contains an automatically updating user-defined list
    /// </summary>
    public class VirtualContactGroup : ContactGroup
    {
        private Predicate<MetaContact> filter;

        /// <summary>
        /// Initializes a new VirtualContactGroup
        /// </summary>
        internal VirtualContactGroup(ContactList parentList, string displayname, ICore core)
            : base(parentList, displayname, core)
        {
        }

        /// <summary>
        /// Gets or sets the predicate that defines the contacts visible in this group
        /// </summary>
        public Predicate<MetaContact> Filter
        {
            get { return filter; }
            set
            {
                if (filter != value)
                {
                    filter = value;
                    if (value != null)
                    {
                        Contacts.Filter(value);
                    }
                    else
                    {
                        Contacts.RemoveFilter();
                    }
                }
            }
        }

        /// <summary>
        /// Gets a value indicating whether this group is virtual (i.e., not created by a protocol)
        /// </summary>
        public override bool IsVirtual
        {
            get { return true; }
        }

        /// <summary>
        /// Gets the number of metacontacts that are visible within this virtual group
        /// </summary>
        /// <remarks>Overriden by the virtual contact group to alias the <see cref="Count"/> property</remarks>
        public override int OnlineCount
        {
            get { return this.Count; }
            internal set { }
        }

        /// <summary>
        /// Gets the number of metacontacts that are visible within this virtual group
        /// </summary>
        public override int Count
        {
            get { return Contacts.Count; }
        }

        /// <summary>
        /// Overriden to prevent updating of the OnlineCount, not used for virtual groups
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected override void contacts_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Items")
            {
                OnPropertyChanged("Count");
            }
        }

        /// <summary>
        /// Returns a string representation of a VirtualContactGroup
        /// </summary>
        public override string ToString()
        {
            return String.Format(CultureInfo.InvariantCulture, "Virtual Group: {0}", DisplayName);
        }
    }
}
